package com.sky.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    public static String toMD5(String password) {
        try {
            // 获取MD5 MessageDigest 实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算MD5值
            md.update(password.getBytes());
            byte[] digest = md.digest();
            // 将字节转换为十六进制表示的字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }

    public static void main(String[] args) {
        String password = "your_password_here";
        String encryptedPassword = toMD5(password);
        System.out.println("Original Password: " + password);
        System.out.println("MD5 Encrypted Password: " + encryptedPassword);
    }
}